<template>
  <div>
    <meta name="format-detection" content="telephone=yes" />

    <div>
      <van-nav-bar title="修改订单收货地址"
                   left-text="返回"
                   @click-left="onClickLeft"
                   left-arrow
      />
    </div>

    <div class="container">
      <div>

        <div class="rownr">
          <div style="font-size: 18px;padding-left: 15px;padding-top: 10px;font-weight: bold">收货地址</div>
          <div style="position:absolute;right: 30px;display: flex;flex-direction: row;font-size: 14px;padding-top: 10px;" @click="onShow">
            <div>切换收货地址</div>
            <div style="padding-top: 5px;">
              <van-icon name="location-o" />
            </div>

          </div>
        </div>

        <van-address-edit
          :area-list="areaList"
          show-postal
          :show-set-default = "false"
          show-search-result
          :address-info="addressInfo"
          :show-postal="false"
          :search-result="searchResult"
          :area-columns-placeholder="['请选择', '请选择', '请选择']"
          @save="saveAgencyAddress"
          @delete="onDelete"
          @change-detail="onChangeDetail"
          :show-delete="false"
          save-button-text="确认修改"
        />

        <van-popup v-model="show" round position="bottom" :style="{ height: '60%' }" >
          <van-address-list
            v-model="chosenAddressId"
            :list="list"
            :disabled-list="disabledList"
            disabled-text="以下地址超出配送范围"
            default-tag-text="默认"

            @add="onAdd"
            @edit="onEdit"
            @select="onSelect"
          />

        </van-popup>



      </div>

    </div>

  </div>
</template>

<script>
  import {queryShareInfo,addShareLy,queryShareLy,addShareStoreInfo ,addLyhfinfo,queryAgencyinfo,queryProductSku ,saveAgencyAddress,queryAddressById ,updateAgencyAddress ,queryAgenyAddress , cancelAgencyOrder } from 'api/api'
  import {Toast,Dialog} from 'vant'
  import AreaList from '../../../static/area'


  export default {
    name: 'addAddress',
    data() {
      return {
        areaList:AreaList,
        agencyId:'',
        addressId:'',
        addressInfo:{},
        orderId:'',
        show:false,
        list:[],
        chosenAddressId:'',
        disabledList:[],
        agencyid:''
      }
    },
    created() {


      let addressId = this.$route.query.addressId
      let orderId = this.$route.query.orderId
      let agencyid = this.$route.query.agencyid
      this.addressId = addressId
      this.orderId = orderId
      this.agencyid = agencyid
      this.chosenAddressId = addressId + ''
      if(addressId){
        this.queryAgenyAddress()
      }
      if(agencyid){
        this.queryAgenyAddressList()
      }

    },
    methods: {

      onClickLeft(){
        this.$router.go(-1) // 返回上一层
      },

      onSave(data){

        console.log('地址：',data )
      },

      saveAgencyAddress(data ){
        let param = {}
        param.orderId = this.orderId
        param.addressId = this.addressInfo.addressId
        cancelAgencyOrder(param).then(response => {
          if(response.code == 200 ){
            //Toast.success('取消成功')
            Dialog.alert({
              message: '订单地址修改成功',
            }).then(() => {
              // on close
              this.$router.go(-1) // 返回上一层
            });


          }
        });

      },

      queryAgenyAddress(){
        let param = {}
        param.addressId  = this.addressId
        queryAddressById(param).then(res =>{
          if(res.code == 200 ){
            this.addressInfo = res.data
            if(res.data ){
              if(res.data.mrflag == '1'){
                this.addressInfo.isDefault = true
              }
            }
          }
        })
      },

      queryAgenyAddressList(){

        let param = {}
        param.agencyId  = this.agencyid
        queryAgenyAddress(param).then(res =>{
          if(res.code == 200 ){
            let list   = res.rows

            if(list.length > 0 ){
              for(var i=0;i<list.length;i++){

                if(list[i].mrflag == '1'){
                  list[i].isDefault  = true
                }

                list[i].address = list[i].province + list[i].city + list[i].county + list[i].addressDetail
                list[i].id = list[i].addressId +''
              }
            }
            this.chosenAddressId = this.addressId + ''
            this.list  = list
          }
        })

      },


      onAdd(){
        this.$router.push({ path: '/addAddress', query:{ agencyId:this.agencyId } })
      },


      onShow(){
        this.show = true
        this.chosenAddressId = this.addressId + ''
      },

      onSelect(item){

        this.addressInfo = item
        this.show = false
      }

    },

  }
</script>

<style scoped>

  .container{
    background-color: white;
  }

  .liuyan{
    background-color: white;
    margin-top: 20px;
    padding-bottom: 60px;
  }
  .columnr{
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
  }

  .rownr{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start;
    width: 100%;
  }

  .name{
    padding-top: 10px;
    padding-left: 10px;
    font-size: 20px;
    color: #466BAC;
  }

  .img{
    padding-top: 10px;
    padding-left: 10px;
  }

  .content{
    font-size: 16px;
    padding:10px;
    text-indent: 2em;
  }

  .viewimg{
    width: 90%;
    padding-top: 10px;
    padding-left: 5%;
    padding-right: 4%;
  }

  .title_bg{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    padding:10px
  }
  .title_sx{
    height: 20px;
    width: 3px;
    background-color: #f50;
  }
  .title_name{
    color:#333333;
    font-size: 20px;
    margin-left: 5px;
    font-weight: bold;
  }

  .hfcontent{
    padding-top: 10px;
    padding-left: 60px;
    font-size: 16px;
  }

  .lyban{
    width: 99%;
    position: fixed;
    bottom: 0;
    height: 50px;
    background-color: #ffffff;
    border-top: 1px solid #B2B2B2 ;
  }

  .lytextcss{
    margin-top:8px;
    margin-left:10px;
    border-radius:15px;
    border: 1px solid #B2B2B2;
    width: 90%;
    height: 30px;
  }

  .textareacss{
    margin-left:10px;
    margin-right:10px;
    margin-top: 10px;
    margin-bottom: 20px;
    border: 1px solid #B2B2B2;
  }

  .hfcss{
    padding-left: 60px;
    padding-top: 10px;
    font-size: 16px;
  }

</style>
